<?php

namespace app\admin\model\zhongtai;

use think\Model;

class User extends Model
{
    // 表名
    protected $table = 'user';
    // 设置当前模型的数据库连接
    protected $connection = 'db_zhongtai';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $deleteTime = false;

    /**
     * 获取最新的更新时间
     */
    public function getUpTime($manage_id)
    {
        $updatetime = $this->where('manage_id', $manage_id)->order("zt_time desc")->value("zt_time");
        return $updatetime ?? 0;
    }

    public function upData($data, $manage_id)
    {
        if ($data) {
            $con = count($data);
            foreach ($data as $k => $v) {
                echo $k . PHP_EOL;
                if (($con - 1) == $k) {
                    $zttime = date('Y-m-d H:i:s',strtotime($v['zt_time'])+1);
                }else{
                    $zttime = $v['zt_time'];
                }
                $in_data = [
                    "manage_id" => $manage_id,
                    "user_id" => $v['id'],
                    "username" => $v['username'],
                    "nickname" => $v['nickname'],
                    "email" => $v['email'],
                    "mobile" => $v['mobile'],
                    "avatar" => $v['avatar'],
                    "gender" => $v['gender'],
                    "birthday" => $v['birthday'],
                    "bio" => $v['bio'],
                    "successions" => $v['successions'],
                    "maxsuccessions" => $v['maxsuccessions'],
                    "prevtime" => $v['prevtime'],
                    "logintime" => $v['logintime'],
                    "loginip" => $v['loginip'],
                    "loginfailure" => $v['loginfailure'],
                    "joinip" => $v['joinip'],
                    "jointime" => $v['jointime'],
                    "zt_time" => $zttime,
                    "status" => $v['status'],
                    "canceled" => $v['canceled'],
                    "is_real" => $v['is_real'],
                    "trade_password" => $v['trade_password'],
                    "staff" => $v['staff'],
                    "prefix_link" => $v['prefix_link'],
                    "is_vip" => $v['is_vip'],
                    "use_status" => $v['use_status'],
                    "vip_exp_time" => $v['vip_exp_time'],
                    "one_day_first" => $v['one_day_first'],
                    "createtime" => $v['createtime'],
                ];
                $user_data = $this->where([
                    'user_id' =>  $v['id'],
                    'manage_id' => $manage_id,
                ])->find();
                if ($user_data) {
                    $this->where([
                        'user_id' =>  $v['id'],
                        'manage_id' => $manage_id,
                    ])->update($in_data);
                } else {
                    $this->insert($in_data);
                }
            }
        }
        echo "user表更新完成";
    }
    public function getUserStatus($manage_id,$user_id){
        $data = $this->where(['manage_id'=>$manage_id,'user_id'=>$user_id])->find();
        if($data){
            return true;
        }else{
            return false;
        }
    }
}
